//
//  OperationQueueShell.h
//  etionLib
//
//  Created by xqls on 12-5-15.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import <Foundation/Foundation.h>

#pragma mark --废弃--

//@interface COperationQueueShell : NSObject
//
//+(void)InitOperationQueue;
//
//+(void)ReleaseOperationQueue;
//
//+(void)AddOperation:(id)target sel:(SEL)sel pararm:(id)pararm;
//
//+(void)AddOperationWithBlock:(void(^)(void))block;
//
//+(void)AddOperations:(NSArray *)ops WaitUntilFinished:(BOOL)wait;
//
//@end

#pragma mark -- 

//异步主线程
extern void DispatchMainThreadAsync(dispatch_block_t handle);

//同步主线程
extern void DispatchMainThreadSync(dispatch_block_t handle);

//间隔n秒后异步主线程
extern void DispatchMainThreadSyncAfterDelay(NSTimeInterval nInterval,dispatch_block_t handle);

//若当前线程是主线程，马上执行，否则按bWaitnuitdone把handle放到主线程中执行
extern void DispatchMainThread(BOOL bWaitunitdone,dispatch_block_t handle);

//在线程中执行，若当前已是主线程，则在新建线程执行，否则在当前线程中执行
extern void DispatchThread(dispatch_block_t handle);

//在新的线程中执行
extern void DispatchGlobalThreadAsync(dispatch_block_t handle);

//在指定线程中执行，若指定线程为主线程，按DispatchMainThread规则，若指定线程为当前线程，规则类似DispatchMainThread，否则按bWaitnuitdone把handle放到线程中执行
extern void DispatchQueueThread(dispatch_queue_t queue,const void *key,BOOL bWaitunitdone,dispatch_block_t handle);

//若当前线程为主线程，马上执行，否则在指定线程中执行，若当前线程与指定线程相同，马上执行
extern void DispatchMainThreadQueue(dispatch_queue_t queue,const void *key, BOOL bWaitunitdone,dispatch_block_t handle);

//在指定线程中执行，若当前线程与指定线程相同，马上执行，否则等待
extern void DispatchThreadSync(dispatch_queue_t queue, const void *key, dispatch_block_t handle);

extern void DispatchThreadGroup(NSArray* arGrouphandle);

typedef NS_ENUM(NSUInteger,EDispatchThreadMultiTaskRunType)
{
    EDispatchThreadMultiTaskRunTypeQueue,
    EDispatchThreadMultiTaskRunTypeGroup
};

extern void DispatchThreadMultiTask(EDispatchThreadMultiTaskRunType type,NSUInteger groupCount,NSArray* arTaskblock,void(^isNeedContuinue)(NSUInteger taskInindex,NSInteger nResult,BOOL *stop));

#pragma mark --

#pragma mark -- GCD线程部分封装 --

@interface XMSDispatchQueueWrapper : NSObject

@property(nonatomic,readonly) dispatch_queue_t queue;
@property(nonatomic,readonly) const void *key;

-(id)init;

-(id)initWithName:(const char *)name;

-(id)initWithName:(const char *)name key:(const void*)key;

+(id)dispatchQueueWrapper;

+(id)dispatchQueueWrapperWithName:(const char *)name;

+(id)dispatchQueueWrapperWithName:(const char *)name key:(const void*)key;

-(void)dispatchThreadSync:(dispatch_block_t)block;

-(void)dispatchThreadAsync:(dispatch_block_t)block;

@end

#pragma mark --

#pragma mark -- 单一线程异步队列 --

/**
 *  单一线程队列
 */

@interface XMSSingleQueue : NSObject

+ (void)runAsyncBlock:(void (^)())block;

+ (void)runSyncBlock:(void (^)())block;

@end

#pragma mark --

#pragma mark -- 静态变量访问队列 --

/**
 *  此类用于解决简单的全局、静态变量的访问保护，及给没有线程安全的类的变量访问时以保护
 */
@interface XMSStaticVariableQueue : NSObject

/**
 *  处于此方法下的访问都是安全的，此方法会阻塞线程(sync)
 *
 *  @param block
 */
+ (void)accessVariable:(void (^)())block;

+ (void)syncAccessVariable:(void (^)())block;

+ (void)asyncAccessVariable:(void (^)())block;

@end


#pragma mark --

@interface XMSWaitingSemaphore : NSObject

+(void)waitUntilSignal:(void(^)(void(^signalFun)()))block;

@end

